package com.newlink.building.userinfo.activity

import android.content.Context
import android.content.Intent
import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.newlink.building.common_base.base.NL_BaseMvpActivity
import com.newlink.building.common_base.ext.showToast
import com.newlink.building.common_base.model.bean.FamilyManagementListData1
import com.newlink.building.common_base.model.bean.UserQrCodeData
import com.newlink.building.common_base.utils.QRCodeUtils
import com.newlink.building.common_base.widget.CustomDialog
import com.newlink.building.userinfo.R
import com.newlink.building.userinfo.databinding.ActivityFamilyManagementDetailWaitActivationQrcodeBinding
import com.newlink.building.userinfo.mvp.Module_UserInfo_FamilyManagementDetailWaitActivationQRCodePresenter
import com.newlink.building.userinfo.mvp.contract.Module_UserInfo_FamilyManagementDetailWaitActivationQRCodeContract

/**
 * @author wangyang
 * @date 2022/10/12
 * @description 我的——家庭管理——等待激活状态的详情页（二维码）页面
 */
class Module_UserInfo_FamilyManagementDetailWaitActivationQRCodeActivityNL :
    NL_BaseMvpActivity<Module_UserInfo_FamilyManagementDetailWaitActivationQRCodeContract.View, Module_UserInfo_FamilyManagementDetailWaitActivationQRCodeContract.Presenter>(),
    Module_UserInfo_FamilyManagementDetailWaitActivationQRCodeContract.View, View.OnClickListener {

    var data: FamilyManagementListData1? = null


    companion object {
        /**
         * 对外开放统一入口
         *
         * @param context
         * @param url     name
         * @param title   phone
         */
        fun startSelf(context: Context?, data: FamilyManagementListData1) {
            val intent =
                Intent(context, Module_UserInfo_FamilyManagementDetailWaitActivationQRCodeActivityNL::class.java)
            intent.putExtra("data", data)
            context?.startActivity(intent)
        }
    }

//    override fun attachLayoutRes(): Int =
//        R.layout.activity_family_management_detail_wait_activation_qrcode

    private lateinit var mBinding: ActivityFamilyManagementDetailWaitActivationQrcodeBinding
    override fun attachLayoutRes(): View {
        mBinding = ActivityFamilyManagementDetailWaitActivationQrcodeBinding.inflate(layoutInflater)
        return mBinding.root
    }

    override fun initData() {
        data = intent?.getSerializableExtra("data") as FamilyManagementListData1?
        mPresenter?.getUserQrCode(accessToken, data?.id ?: "")
    }

    override fun initView() {
        super.initView()
        findViewById<TextView>(R.id.tv_title_bar_center).text =
            resources.getText(R.string.family_management_detail_qrcode_title)
        findViewById<ImageView>(R.id.iv_title_bar_right).setBackgroundResource(R.mipmap.title_bar_delete)
        if (isPrimaryAccount) {
            findViewById<ImageView>(R.id.iv_title_bar_right).visibility = View.VISIBLE
        } else {
            findViewById<ImageView>(R.id.iv_title_bar_right).visibility = View.GONE
        }
        findViewById<ImageView>(R.id.iv_title_bar_right).setOnClickListener(this)


        mBinding.tvUsername.text = data?.name
        mBinding.tvPhone.text = data?.aid.toString()
        if (TextUtils.isEmpty(data?.email)) {
            mBinding.rlEmail.visibility = View.GONE
        } else {
            mBinding.rlEmail.visibility = View.GONE
            mBinding.tvEmail.text = data?.email
        }
        mBinding.ivQrcode.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.iv_title_bar_right -> {
                //删除
                CustomDialog(this).setMessage(getString(R.string.family_management_delete))
                    .setPositiveButton(object : CustomDialog.OnClickListener {
                        override fun onClick(dialog: CustomDialog) {
                            data?.id?.let { mPresenter?.deleteFamilyManagement(accessToken, it) }
                            dialog.dismiss()
                        }
                    }).setNegativeButton(object : CustomDialog.OnClickListener {
                        override fun onClick(dialog: CustomDialog) {
                            dialog.dismiss()
                        }
                    }).show()
            }

            R.id.iv_qrcode -> {
                //刷新二维码
            }
        }
    }

    override fun createPresenter(): Module_UserInfo_FamilyManagementDetailWaitActivationQRCodeContract.Presenter =
        Module_UserInfo_FamilyManagementDetailWaitActivationQRCodePresenter()

    override fun deleteFamilyManagementSuccess() {
        showToast(getString(R.string.default_delete_success))
        finish()
    }

    override fun deleteFamilyManagementFail() {
        showToast(getString(R.string.default_delete_fail))
    }

    override fun getUserQrCodeSuccess(qrCode: UserQrCodeData) {
        mBinding.ivQrcode.setImageBitmap(
            QRCodeUtils.createQRCode(
                qrCode.qrCode,
                mBinding.ivQrcode.width
            )
        )
        mBinding.tvUsername.text = qrCode?.name
        mBinding.rlNumber.visibility =
            if (data?.aid.toString().isNullOrEmpty()) View.GONE else View.GONE
    }

    override fun getUserQrCodeFailed() {
        showToast(getString(R.string.family_management_get_qr_code_failed))
    }
}